package DBLayer;
import java.sql.*;
import java.util.ArrayList;
import ModelLayer.CustomerGroup;

public class DBCustomerGroup implements IFDBCustomerGroup
{
    private Connection con;
    
    public DBCustomerGroup() 
    {
        con = DBConnection.getInstance().getDBcon();
    }
    
    @Override
    public CustomerGroup findCustomerGroup(int customerGroupID, boolean retriveAssociation)
    {
        CustomerGroup customerGroupObj = new CustomerGroup();
        customerGroupObj = singleWhere("cgId = " + customerGroupID, retriveAssociation);
        
        return customerGroupObj;
    }
    
    private CustomerGroup singleWhere(String wClause, boolean retrieveAssociation)
    {	
        ResultSet results;
        CustomerGroup customerGroupObj = new CustomerGroup();
        String query =  buildQuery(wClause);
        
        try
        {
	   Statement stmt = con.createStatement();
	   stmt.setQueryTimeout(5);
	   results = stmt.executeQuery(query);
           
	   if(results.next())
           {
	       customerGroupObj = buildCustomerGroup(results);
           }
           
	   stmt.close();
	}
	catch(Exception e)
        {
        
        }
        
	return customerGroupObj;
    }
    
    private ArrayList<CustomerGroup> miscWhere(String wClause, boolean retrieveAssociation)
    {
        ResultSet results;
        ArrayList<CustomerGroup> list = new ArrayList<CustomerGroup>();

        String query =  buildQuery(wClause);
        
        try
        { 
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            while(results.next())
            {
                 CustomerGroup customerGroupObj = new CustomerGroup();
                 customerGroupObj = buildCustomerGroup(results);	
                 list.add(customerGroupObj);
            }

            stmt.close();
        }
        catch(Exception e)
        {

        }

        return list;
    }
    
    private CustomerGroup buildCustomerGroup(ResultSet results)
    {
        CustomerGroup customerGroupObj = new CustomerGroup();
        
        try
        {
           customerGroupObj.setCustomerGroupID(results.getInt(1)); 
           customerGroupObj.setDescription(results.getString(2));
           customerGroupObj.setType(results.getBoolean(3));
           customerGroupObj.setCredit(results.getDouble(4));
           customerGroupObj.setCvr(results.getInt(5)); 
           
        }
        catch(Exception e)
        {

        }
        
        return customerGroupObj;
    }
 
    private String buildQuery(String wClause)
    {
        String query="SELECT * FROM CustomerGroup";

        if (wClause.length() > 0)
        {
            query = query + " WHERE " + wClause;
        }
        
        return query;
    }

    @Override
    public ArrayList getAllCustomerGroup(boolean retriveAssociation) 
    {
        ArrayList list = new ArrayList();
        
        return list;
        
        // Manglende kode
    }

    @Override
    public void insertCustomerGroup(CustomerGroup customerGroupObj)
    {  
        String query = "INSERT INTO CustomerGroup(description, type, credit, cvr) "
                + "VALUES"
                + "('" 
                 + customerGroupObj.getDescription() + "', " +
                "'"+ customerGroupObj.getType() + "', " +
                "'"+ customerGroupObj.getCredit() + "', " +
                "'" + customerGroupObj.getCvr() + "')";
            
      System.out.println("Update query: " + query);
           try
        { 
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);

            stmt.close();
        }
        catch(Exception ex)
        {
            
        }
    }

    @Override
    public void updateCustomerGroup (CustomerGroup customerGroupObj)
    {
        String query="UPDATE CustomerGroup SET "+
                "description = '" + customerGroupObj.getDescription() + "', " +
                "type ='"+ customerGroupObj.getType() + "', " +
                "credit ='"+ customerGroupObj.getCredit() + "', " +
                "cvr ='" + customerGroupObj.getCvr() +
                " WHERE customerGroupID = '" + customerGroupObj.getCustomerGroupID() + "'";
        
           System.out.println("Update query: " + query);
   
        try
        { 
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);

            stmt.close();
        }
        catch(Exception ex)
        {

        }
    }

    @Override
    public void deleteCustomer(int customerGroupID)
    {
        String query = "DELETE FROM CustomerGroup WHERE customerGroupID = '" +
                        customerGroupID + "'";

        try
        { 
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }
        catch(Exception ex)
        {

        }
    }
}